package yxy.game.pm2.command.therionEquipment;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Item;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PickedGodBeastMessage;
import yxy.game.pm2.module.godBeast.TherionEquipHandler;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author wrs
 * @since 2022-07-28
 * @descrition 神兽装备升级信息界面
 */
@MessageType(proto = Command.TherionEquipmentInfo ,classtype = PickedGodBeastMessage.TherionEquipmentInfoRequest.class)
public class CMD_TherionEquipmentInfo extends Command<PickedGodBeastMessage.TherionEquipmentInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, PickedGodBeastMessage.TherionEquipmentInfoRequest request) throws Exception {
        String playerCode = messageSource.getTag();

        PickedGodBeastMessage.TherionEquipmentInfoResponse.Builder response = PickedGodBeastMessage.TherionEquipmentInfoResponse.newBuilder();

        TherionEquipHandler handler = new TherionEquipHandler();

        try {
            Collection<Currency> equips = Item.all(playerCode, CurrencyType.CT_装备).values();// 玩家背包中的装备

            if (equips == null){
                send(messageSource,response, Result.DataError);
                return;
            }

            ArrayList<Currency> therionEquips = new ArrayList<>(); //拥有的神兽装备

            //过滤武将装备
            for (Currency equip : equips) {
                if (equip.getDataId() >= 100001 && equip.getDataId() <= 102010){
                    for (long i = 0; i < equip.getNumber(); i++) {
                        therionEquips.add(equip);
                    }
                }
            }

            Result result = handler.packTherionEquips(therionEquips,response);

            send(messageSource,response,result);

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private void send(MessageSource messageSource, PickedGodBeastMessage.TherionEquipmentInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
